package cn.sz.hy.logic.strategy;

import java.awt.TextField;
import java.util.HashMap;
import java.util.Map;

import cn.sz.hy.logic.strategy.impl.InsertStrategy;
import cn.sz.hy.logic.strategy.impl.XlsStrategy;
import cn.sz.hy.logic.strategy.vo.BasicVo;

public class StrategyFactory {
	private TextField src1;
	private TextField src2;
	private TextField det;
	private static  StrategyFactory sf = new StrategyFactory();
	private static final Map<String,XlsStrategy> mapping = new HashMap<String, XlsStrategy>();
	private static String[] typeToName = new String[2];
	static{
		typeToName[0] = InsertStrategy.class.getName();
	}
	
	private StrategyFactory(){
		
	}
	
	public static StrategyFactory getInstance(){
		return sf;
	}
	
	public void setSrc1(TextField src1) {
		this.src1 = src1;
	}



	public void setSrc2(TextField src2) {
		this.src2 = src2;
	}



	public void setDet(TextField det) {
		this.det = det;
	}

	public BasicVo getEntity(){
		BasicVo be = new BasicVo();
		be.setDet(det.getText());
		be.setSrc1(src1.getText());
		be.setSrc2(src2.getText());
		return be;
	}
	
	public synchronized static XlsStrategy getStrategy(int type){
		return getStrategy(typeToName[type]);
	}

	public synchronized static XlsStrategy getStrategy(String name){
		try {
			if(mapping.containsKey(name)){
				return mapping.get(name);
			}
			Class<?> cls = Class.forName(name);
			Object strategyObj = cls.newInstance();
			if(strategyObj instanceof ExecuteStrategy){
				mapping.put(name, (XlsStrategy) strategyObj);
				return (XlsStrategy) strategyObj;
			}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
}
